/*
 * Copyright 2010 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy.anim.impl
{

	internal class PaddingAnimation extends AnimationDecoratorBase
	{

		private var _after:uint;

		private var _before:uint;

		private var _realDuration:uint;

		public function PaddingAnimation(manager:AnimationManager, animation:AnimationBase, before:uint, after:uint)
		{
			super(manager, animation);

			_before = before;
			_after = after;
		}

		override public function get parameters():Object
		{
			return {before: _before, after: _after};
		}

		override protected function get animationName():String
		{
			return "Padding";
		}

		override protected function onInitializeInternal():void
		{
			_targetAnimation.initialize();
			_realDuration = _targetAnimation._duration;

			if (_realDuration + _before + _after > int.MAX_VALUE)
			{
				var diff:int = _realDuration + _before + _after - int.MAX_VALUE;
				if (diff > _before)
				{
					diff -= _before;
					_before = 0;
					_after -= diff;
				}
				else
				{
					_before -= diff;
				}
			}

			_duration = _realDuration + _before + _after;
		}

		override protected function onUpdateInternal():void
		{
			var realPosition:int = _position - _before;
			if (realPosition < 0)
			{
				return;
			}
			else if (realPosition > _duration)
			{
				if (_targetAnimation._position === _realDuration)
				{
					return;
				}
				else
				{
					_targetAnimation.position = _realDuration;
				}
				return;
			}

			_targetAnimation.position = realPosition;
		}
	}
}
